<?php
/**
 * This file defines the {@see \BF\Validation\IntegerValidator} class.
 *
 * @author     Kado <ulfikado@gmail.com>
 * @category   Beluga PHP5.3+ Framework
 * @package    BF\Validation
 * @since      2015-01-31 00:29
 * @subpackage Validators
 * @version    0.1
 */

namespace BF\Validation
{

   /**
    * This class defines a validator that allows only valid integer values
    *
    * @property integer|NULL $MinValue The minimal allowed int value or NULL if no min value should be used (default=1)
    * @property integer|NULL $MaxValue The max allow value value or NULL if no max should used (default=\PHP_INT_MAX)
    * @since    v0.1
    */
   class IntegerValidator extends Validator
   {

      # <editor-fold defaultstate="collapsed" desc=" - - >   P U B L I C   C O N S T R U C T O R   - - - - - - - - - - - - - - - - - -">

      /**
       * Inits a new instance.
       *
       * Usable options are:
       *
       * - <b>MinValue</b>: The minimal allowed integer value or NULL if no min value should be used (default=1)
       * - <b>MaxValue</b>: The maximal allowed integer value or NULL if no max value should be used (default=\PHP_INT_MAX)
       *
       * @param  array   $options  All validator options
       */
      public function __construct( array $options = array() )
      {

         self::setIntegerOption( 'MinValue',               $options, null, true );
         self::setIntegerOption( 'MaxValue',               $options, null, true );

         $options[ 'Result' ] = 0;

         parent::__construct( $options );

      }

      # </editor-fold>

      # <editor-fold defaultstate="collapsed" desc=" - - -   P U B L I C   M E T H O D S   - - - - - - - - - - - - - - - - - - - - - -">

      /**
       * Sets a option value.
       *
       * @param string $optionName The name of the option to set the value at.
       * @param mixed  $value      The new value. (the value type depends to the option)
       */
      public function setOption( $optionName, $value )
      {

         switch ( $optionName )
         {
            case 'MinValue':
            case 'MaxValue':
               if ( \BF\TypeTool::IsInteger( $value ) )
               {
                  $this->_options[ $optionName ] = \intval( $value );
               }
               elseif ( \is_null( $value ) )
               {
                  $this->_options[ $optionName ] = null;
               }
               $this->LastResult = null;
               break;

            default:
               parent::setOption( $optionName, $value );
               break;

         }

      }

      # </editor-fold>

      # <editor-fold defaultstate="collapsed" desc=" - - -   P R O T E C T E D   M E T H O D S   - - - - - - - - - - - - - - - - - - -">

      protected function __validate()
      {

         $this->_message = '';

         if ( ! \BF\TypeTool::IsInteger( $this->_value ) )
         {

            $this->_message = \BF\_1(
               '_Validation',
               'This is not a valid %s!',
               \BF\_0( '_Validation', 'integer value' )
            );

            return $this->__setLastResult( false );

         }

         $this->_options[ 'Result' ] = \intval( $this->_value );

         if ( isset( $this->_options[ 'MinValue' ] ) && $this->_options[ 'Result' ] < $this->_options[ 'MinValue' ] )
         {

            $this->_message = \BF\_1(
               '_Validation',
               'The value is lower than allowed! (Allowed minimal value is %d)',
               $this->_options[ 'MinValue' ]
            );

            return $this->__setLastResult( false );

         }



         if ( isset( $this->_options[ 'MaxValue' ] ) && $this->_options[ 'Result' ] > $this->_options[ 'MaxValue' ] )
         {

            $this->_message = \BF\_1(
               '_Validation',
               'The value is bigger than allowed! (Allowed maximal value is %d)',
               $this->_options[ 'MaxValue' ]
            );

            return $this->__setLastResult( false );

         }

         return $this->__setLastResult( true );

      }

      # </editor-fold>

   }

}


